import urllib2
import urllib
import random
import time
import requests

def method_post(url,data,header):
	# needn't use urlencode
	req = urllib2.Request(url,data,header)
	resp = urllib2.urlopen(req)
	return resp.read()
	
if __name__ == '__main__':
	url = 'http://api.yeelink.net/v1.0/device/9966/sensor/19877/datapoints'
	
	while True:
		header={'U-ApiKey':'d3d565a59151cd21f3afdd82105e0e5a','content-type': 'application/json'}
		data = random.randint(0,100)
		data = '{"value":' + str(data) + '}'
		print 'posting data to yeelink: ' + str(data)
		method_post(url,data,header)
		
		url = 'http://api.yeelink.net/v1.0/device/9966/sensor/23363/photos'
		data = open('image/gif/morning.gif','rb').read()
		header = {'U-ApiKey':'d3d565a59151cd21f3afdd82105e0e5a', \
			'Cache-Control':'no-cache','Content-Type':'image/gif','Content-Length': len(data)}
		ret = requests.post(url,data=data,headers=header)
		print 'ret:',ret
		time.sleep(40)
		
	print 'hello,yeelink'
	raw_input()
